package com.cmc.cloud.cmclink.doc.service.impl.special;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SpecialCargoApplyErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SpecialCargoFlexiErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.entity.SpecialCargoApplyDO;
import com.cmc.cloud.cmclink.doc.entity.SpecialCargoCntrDO;
import com.cmc.cloud.cmclink.doc.entity.SpecialCargoFlexiDO;
import com.cmc.cloud.cmclink.doc.entity.SpecialCargoVoyageDO;
import com.cmc.cloud.cmclink.doc.mapper.SpecialCargoFlexiMapper;
import com.cmc.cloud.cmclink.doc.service.special.SpecialCargoValidateService;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.cmc.cloud.cmclink.doc.constants.SepcialCargoConstans.*;

/**
 * Flexi类型货物校验器
 * @author: 申天青
 * @date: 2025/8/4
 */
@Service
public class FlexiCargoValidateServiceImpl implements SpecialCargoValidateService {

    /**
     * 集装箱类型常量
     */
    private static final String CONTAINER_TYPE_GP = "GP";
    private static final String CONTAINER_TYPE_RF = "RF";


    /**
     * 集装箱尺寸常量
     */
    private static final String CONTAINER_SIZE_20 = "20";
    // 20英尺集装箱

    @Resource
    private SpecialCargoFlexiMapper specialCargoFlexiMapper;
    public void validate(Long applyId, SpecialCargoApplyDO applyDO, List<SpecialCargoCntrDO> cntrDOList, List<SpecialCargoVoyageDO> specialCargoVoyageDos) {
        cntrDOList.forEach(cntr -> {
            //FLEXI	Container Info	Type	仅GP 、RF两种箱型可以申请
            if (!CONTAINER_TYPE_GP.equals(cntr.getCntrType()) && !CONTAINER_TYPE_RF.equals(cntr.getCntrType())){
                throw ServiceExceptionUtil.exception(SpecialCargoFlexiErrorCodeConstants.SPECIAL_CARGO_FLEXI_TYPE_ERROR);
            }
            //FLEXI	Container Info	Size	仅20才能提交，液袋货只接受20的
            if (!CONTAINER_SIZE_20.equals(cntr.getCntrSize())) {
                throw ServiceExceptionUtil.exception(SpecialCargoFlexiErrorCodeConstants.SPECIAL_CARGO_FLEXI_SIZE_ERROR);
            }
            //FLEXI	Container Info	Net WGT	<=24000KG
            if (cntr.getNetWgt().compareTo(DEFAULT_FLEXI_NET_WGT) > 0) {
                throw ServiceExceptionUtil.exception(SpecialCargoFlexiErrorCodeConstants.SPECIAL_CARGO_FLEXI_NET_WGT_ERROR);
            }

            SpecialCargoFlexiDO flexiDO = specialCargoFlexiMapper.selectOne(new LambdaQueryWrapper<SpecialCargoFlexiDO>().eq(SpecialCargoFlexiDO::getCntrId, cntr.getId()));
            if (null == flexiDO) {
                throw ServiceExceptionUtil.exception(SpecialCargoApplyErrorCodeConstants.SPECIAL_NO_FLEXI_ERROR);
            }else{
                //FLEXI	FLEXI Info	Volume of cargo (litres)液袋货的体积（升 L）	<=24000L
                if (flexiDO.getVolumeOfCargo().compareTo(DEFAULT_FLEXI_VOLUME_OF_CARGO) > 0) {
                    throw ServiceExceptionUtil.exception(SpecialCargoFlexiErrorCodeConstants.SPECIAL_CARGO_FLEXI_VOLUME_ERROR);
                }
                //FLEXI	FLEXI Info	Loading ratio (%)液体最多能装多少比例	<=95%
                if (flexiDO.getLoadingRatio().compareTo(DEFAULT_FLEXI_LOADING_RATIO) > 0) {
                    throw ServiceExceptionUtil.exception(SpecialCargoFlexiErrorCodeConstants.SPECIAL_CARGO_FLEXI_LOADING_RATIO_ERROR);
                }
            }
        });

    }

}
